package com.ruoyi.classroom.service.impl;

import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.classroom.mapper.classroomMapper;
import com.ruoyi.classroom.domain.classroom;
import com.ruoyi.classroom.service.IclassroomService;

/**
 * 教学资源管理Service业务层处理
 * 
 * @author chenxl
 * @date 2025-04-11
 */
@Service
public class classroomServiceImpl implements IclassroomService 
{
    @Autowired
    private classroomMapper classroomMapper;

    /**
     * 查询教学资源管理
     * 
     * @param id 教学资源管理主键
     * @return 教学资源管理
     */
    @Override
    public classroom selectclassroomById(String id)
    {
        return classroomMapper.selectclassroomById(id);
    }

    /**
     * 查询教学资源管理列表
     * 
     * @param classroom 教学资源管理
     * @return 教学资源管理
     */
    @Override
    public List<classroom> selectclassroomList(classroom classroom)
    {
        return classroomMapper.selectclassroomList(classroom);
    }

    /**
     * 新增教学资源管理
     * 
     * @param classroom 教学资源管理
     * @return 结果
     */
    @Override
    public int insertclassroom(classroom classroom)
    {
        return classroomMapper.insertclassroom(classroom);
    }

    /**
     * 修改教学资源管理
     * 
     * @param classroom 教学资源管理
     * @return 结果
     */
    @Override
    public int updateclassroom(classroom classroom)
    {
        return classroomMapper.updateclassroom(classroom);
    }

    /**
     * 批量删除教学资源管理
     * 
     * @param ids 需要删除的教学资源管理主键
     * @return 结果
     */
    @Override
    public int deleteclassroomByIds(String[] ids)
    {
        return classroomMapper.deleteclassroomByIds(ids);
    }

    /**
     * 删除教学资源管理信息
     * 
     * @param id 教学资源管理主键
     * @return 结果
     */
    @Override
    public int deleteclassroomById(String id)
    {
        return classroomMapper.deleteclassroomById(id);
    }

    /**
     * 导入数据
     *
     * @param classroomList 数据列表
     * @param updateSupport 是否更新支持，如果已存在，则进行更新数据
     * @return 结果
     */
    @Override
    public String importclassroom(List<classroom> classroomList, Boolean updateSupport)
    {
        if (StringUtils.isEmpty(classroomList) || classroomList.size() == 0)
        {
            throw new RuntimeException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (classroom classroom : classroomList)
        {
            try
            {
                if (!updateSupport){
                    classroom.setId(IdUtils.fastSimpleUUID());
                    classroomMapper.insertclassroom(classroom);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、课程 " + classroom.getClassRoomName() + " 导入成功");
                }
                else
                {
                    classroomMapper.updateclassroom(classroom);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、课程 " + classroom.getClassRoomName() + " 更新成功");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、课程 " + classroom.getClassRoomName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
